package encaps.action;

import encaps.entities.Grant;
import encaps.entities.Person;
import encaps.entities.PersonLocal;
import encaps.entities.PersonLocalHome;
import encaps.logging.JMSUtil;
import encaps.logging.Log;
import encaps.resources.Config;
import encaps.resources.Image;
import encaps.resources.Labels;
import encaps.resources.Response;
import encaps.utils.*;
import java.util.Date;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import org.jdom.Element;

public class CreatePersonAction extends ShowEmptyPersonPageAction {

    public static final String CREATE_PERSON = "createPerson";

    public CreatePersonAction(ActionContext context, Grant requiredGrant) {
        super(context, requiredGrant);
    }

    @Override
    public Element execute() throws ActionExecutionException {
        try {
            Element dataToReenter = validate();
            if (dataToReenter != null) {
                return dataToReenter;
            }
            String name = actionContext.getTextParameter(PERSON_NAME);
            String bio = actionContext.getTextParameter(PERSON_BIOGRAPHY);
            //System.out.println("name: "+name+" bio:"+bio);
            PersonLocalHome personHome = EntityUtil.getPersonHome();
            PersonLocal person = personHome.create(name, null, bio);

            String picName = String.format("%d", person.getId());
            String picPath = IOHelper.uploadFile(actionContext, PERSON_PIC, picName, Config.getPersonImagesDirectory());


            if (picPath != null) {
                person.setPhoto(picPath);
            }

            //movies = new HashSet();

            //TODO: Считывание зависимостей MoviePerson

            //person.setMovies(movies);
            JMSUtil.sendLogMessage(Log.INSERT,
                    EntityUtil.getUserHome().findByLogin(actionContext.getUserLogin()).getID(),
                    actionContext.getUserLogin(), new Date(), Person.TABLE_NAME,
                    person.getId(), person.getName());

            //return new ViewPersonAction(actionContext, null).execute();
            return XMLHelper.createTextMessage(Labels.getLabel(Labels.PERSON_CREATED_MESSAGE));

        } catch (CreateException ex) {
            throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
        } catch (SystemException ex) {
            throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
        } catch (NamingException ex) {
            throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
        } catch (FinderException ex) {
            throw new ActionExecutionException("Date conversion exception", ex);
        }
    }

    private Element validate() throws SystemException {
        String name = actionContext.getTextParameter(PERSON_NAME);
        if (!StringUtil.isEmpty(name)) {
            return null;
        }
        Element result = XMLHelper.createElement(Response.RESPONSE);
        Element e = XMLHelper.addChild(result, Response.ENTITY);
        e.setAttribute(Response.EDIT, "true");

        XMLHelper.addChild(e, XMLHelper.createHeader(Labels.getLabel(Labels.CREATE_PERSON)));
        XMLHelper.addChild(e, XMLHelper.createImage("", PERSON_PIC));
        XMLHelper.addChild(e, XMLHelper.createTextField(Labels.getLabel(Labels.PERSON_NAME), name, PERSON_NAME, Labels.getLabel(Labels.EMPTY_NAME_FIELD_MESSAGE)));
        XMLHelper.addChild(e, XMLHelper.createTextArea(Labels.getLabel(Labels.PERSON_BIOGRAPHY), actionContext.getTextParameter(PERSON_BIOGRAPHY), PERSON_BIOGRAPHY));

        XMLHelper.addChild(e, XMLHelper.createImageButton(Image.getImage(Image.CREATE_PERSON), CreatePersonAction.CREATE_PERSON));
        return result;
    }
}
